﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Entities;
using Reviewer.Business;
using System.Collections.Generic;
using System.Globalization;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public int CurrentLetterFilter
    {
        get
        {
            if (ViewState["CurrentLetterFilter"] == null)
            {
                return -1;
            }

            return (int)ViewState["CurrentLetterFilter"];
        }
        set
        {
            ViewState["CurrentLetterFilter"] = value;
        }
    }

    public int CurrentFormatFilter
    {
        get
        {
            if (ViewState["CurrentFormatFilter"] == null)
            {
                return -1;
            }

            return (int)ViewState["CurrentFormatFilter"];
        }
        set
        {
            ViewState["CurrentFormatFilter"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindLatest();
        }

        BindLetters();
        BindFormats();
    }

    private void BindLetters()
    {
        using (IMetadataService<NewsType> service = ServiceFactory.GetMetadataService<NewsType>())
        {
            this.uiRepLetters.DataSource = service.GetAll();
            this.uiRepLetters.DataBind();
        }
    }

    private void BindFormats()
    {
        using (IMetadataService<Format> service = ServiceFactory.GetMetadataService<Format>())
        {
            this.uiRepFormats.DataSource = service.GetAll();
            this.uiRepFormats.DataBind();
        }
    }

    private void BindLatest()
    {
        uiLblHeadline.Text = "Seneste nyheder";

        using (INewsService service = ServiceFactory.NewsService)
        {
            this.uiRepResults.DataSource = service.Search(null, null, 20);
            this.uiRepResults.DataBind();
        }
    }

    private void BindSearch()
    {
        using (INewsService service = ServiceFactory.NewsService)
        {
            this.uiRepResults.DataSource = service.Search(this.CurrentLetterFilter, this.CurrentFormatFilter, 20);
            this.uiRepResults.DataBind();
        }

        if (this.CurrentLetterFilter < 1 && this.CurrentFormatFilter < 1)
        {
            uiLblHeadline.Text = "Seneste nyheder";
        }
        else
        {
            if (this.CurrentLetterFilter > 0 && this.CurrentFormatFilter < 1)
            {
                uiLblHeadline.Text = "Søgning på " + HelperUtilities.GetNewsTypeName(this.CurrentLetterFilter);
            }
            else if (this.CurrentLetterFilter > 0 && this.CurrentFormatFilter > 0)
            {
                using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
                {
                    uiLblHeadline.Text = "Søgning på " + HelperUtilities.GetNewsTypeName(this.CurrentLetterFilter) + " til " + HelperUtilities.GetFormatName(this.CurrentFormatFilter);
                }
            }
            else
            {
                using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
                {
                    uiLblHeadline.Text = "Alle nyheder til " + HelperUtilities.GetFormatName(this.CurrentFormatFilter);
                }
            }
        }
    }

    public void uiLbLetter_Click(object sender, EventArgs e)
    {
        int newLetterFilter = int.Parse((sender as LinkButton).CommandArgument);
        if (this.CurrentLetterFilter.Equals(newLetterFilter))
        {
            this.CurrentFormatFilter = -1;
            this.CurrentLetterFilter = -1;
        }
        else
        {
            this.CurrentLetterFilter = newLetterFilter;
        }
        BindSearch();
    }

    public void uiLbFormat_Click(object sender, EventArgs e)
    {
        int newFormatFilter = int.Parse((sender as LinkButton).CommandArgument);
        if (this.CurrentFormatFilter.Equals(newFormatFilter))
        {
            this.CurrentFormatFilter = -1;
        }
        else
        {
            this.CurrentFormatFilter = newFormatFilter;
        }
        BindSearch();
    }

    protected void uiRepLetters_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                LinkButton link = e.Item.FindControl("uiLbLetter") as LinkButton;
                if (link != null)
                {
                    if (link.ToolTip.Contains("{0}"))
                    {
                        link.ToolTip = String.Format(CultureInfo.InvariantCulture, link.ToolTip, (e.Item.DataItem as NewsType).Name);
                    }
                }
                break;
        }
    }

    protected void uiRepFormats_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                LinkButton link = e.Item.FindControl("uiLbFormat") as LinkButton;
                if (link != null)
                {
                    if (link.ToolTip.Contains("{0}"))
                    {
                        link.ToolTip = String.Format(CultureInfo.InvariantCulture, link.ToolTip, (e.Item.DataItem as Format).Name);
                    }
                }
                break;
        }
    }

    /*
        protected void Page_Load(object sender, EventArgs e)
        {
            int first = 0;
            string formatId = Request.QueryString["Format"];
            string searchKey = Request.QueryString["Ord"];
            string subject = Request.QueryString["Emne"];

            HyperLinkUpdate.NavigateUrl = "?Emne=Update";
            HyperLinkScreenshots.NavigateUrl = "?Emne=Screenshots";
            HyperLinkRygte.NavigateUrl = "?Emne=Rygte";
            HyperLinkPressemeddelse.NavigateUrl = "?Emne=Pressemeddelelse";
            HyperLinkNyUdgivelse.NavigateUrl = "?Emne=Ny Udgivelse";
            HyperLinkNyhed.NavigateUrl = "?Emne=Nyhed";

            HyperLinkXbox.NavigateUrl = "?Navn=Xbox 360&Format=1";
            HyperLinkPC.NavigateUrl = "?Navn=PC&Format=2";
            HyperLinkPlayStation3.NavigateUrl = "?Navn=Playstation 3&Format=3";
            HyperLinkNintendoWii.NavigateUrl = "?Navn=Nintendo Wii&Format=4";

            News[] newsItems = null;

            using (INewsService service = ServiceFactory.NewsService)
            {

                if (searchKey == null && subject == null && formatId == null)
                {
                    newsItems = service.GetLatest(20);
                    uiLblHeadline.Text = "Seneste 20 nyheder";
                }

                if (subject != null && searchKey == null && formatId == null)
                {
                    newsItems = service.GetAll()
                        .Where(t => t.NewsType.ToUpperInvariant().Equals(subject.ToUpperInvariant(), StringComparison.Ordinal))
                        .OrderByDescending(t => t.NewsDate)
                        .Take(20).ToArray();

                    if (subject == "Nyhed")
                    {
                        uiLblHeadline.Text = "Seneste 20  nyheder";
                    }
                    else if (subject == "Pressemeddelelse")
                    {
                        uiLblHeadline.Text = "Seneste 20 pressemeddelelser";
                    }
                    else if (subject == "Screenshots")
                    {
                        uiLblHeadline.Text = "Seneste 20 screenshots";
                    }
                    else if (subject == "Update")
                    {
                        uiLblHeadline.Text = "Seneste 20 updates";
                    }
                    else if (subject == "Ny Udgivelse")
                    {
                        uiLblHeadline.Text = "Seneste 20 nye udgivelser";
                    }
                    else if (subject == "Rygte")
                    {
                        uiLblHeadline.Text = "Seneste 20 rygter";
                    }
                }

                if (formatId != null)
                {

                    using (IFormatService formatService = ServiceFactory.FormatService)
                    {
                        Format format = formatService.GetAll().FirstOrDefault(t => t.Id.Equals(int.Parse(formatId)));
                        if (format != null)
                        {
                            newsItems = service.GetAllByFormat(format.Id);
                            uiLblHeadline.Text = "Seneste 20  " + format.Name + " nyheder";
                        }
                    }
                }

                if (searchKey != null)
                {
                    newsItems = service.Search(searchKey);
                    uiLblHeadline.Text = "Søgning: " + searchKey;
                }

                DivResult.InnerHtml += "<table width=\"100%\">";
                foreach (News item in newsItems)
                {
                    if (first == 0)
                    {
                        DivResult.InnerHtml += "<tr><td class=\"TDrowTwo\"><a href=\"" + UrlUtillity.GetNewsUri(item) + "\">" + item.Title + "</a></td><td class=\"TDrowTwo\">" + item.NewsType + "</td><td class=\"TDrowTwo\">" + item.NewsDate.ToShortDateString() + "</td></tr>";
                        first++;
                    }
                    else
                    {
                        DivResult.InnerHtml += "<tr><td><a href=\"" + UrlUtillity.GetNewsUri(item) + "\">" + item.Title + "</a></td><td>" + item.NewsType + "</td><td>" + item.NewsDate.ToShortDateString() + "</td></tr>";
                        first = 0;
                    }
                }
                DivResult.InnerHtml += "</table>";
            }
        }
     * */
}
